﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SJ.Platform.Common;
using SJ.Platform.EFCore.DBOperation.DBOperation.MySQL.Sys;
using SJ.Platform.EFCore.Model;
using SJ.Platform.EFCore.Model.LogManager;
using SJ.Platform.Extend.Response;
using System.Reflection.Metadata;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace SJ.Platform.LogApi.Controllers
{
    /// <summary>
    /// 操作记录接口类
    /// </summary>
    [Route(GlobalVariableHandle.Scope_LogApi+ "/Operation")]
    [ApiController]
    [Authorize(GlobalVariableHandle.Scope_LogApi)]
    public class OperationController : ControllerBase
    {
        private readonly Sys_LogHandle sys_LogHandle;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext">数据库</param>
        public OperationController(SJDBContext dbContext)
        {
            sys_LogHandle = new Sys_LogHandle(dbContext);
        }

        /// <summary>
        /// 新增操作记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("CreateOperationAsync")]
        public async Task<IActionResult> CreateOperationAsync([FromBody] SysLog model)
        {
            var Menu = new SysLog
            {
                Id = Guid.NewGuid().ToString(),
                ActionName = model.ActionName,
                UserId = model.UserId,
                Details = model.Details,
                Ip = model.Ip,
                AddTime = DateTime.Now
            };
            try
            {
                //await sys_LogHandle.CreateAsync(Menu);
                return Ok(ApiNormalResponse<bool>.SetResponse(ApiResponseStatus.Success, "操作记录添加成功", true));
            }
            catch (Exception ex)
            {
                // 处理异常情况
                return BadRequest(ApiNormalResponse<bool>.SetResponse(ApiResponseStatus.BadRequest, "操作记录添加失败：" + ex.Message, false));
            }
        }
    }
}
